package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.domain.User;
import com.tanhua.model.mongo.RecommendUser;

import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score"))).limit(1);
        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        long count = mongoTemplate.count(query, RecommendUser.class);
        query.skip((page-1)*pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("score")));
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);

        return new PageResult(page,pagesize,count,recommendUsers);
    }

    @Override
    public RecommendUser byuserid(Long userid, Long userId) {
        Query query = Query.query(Criteria.where("toUserId").is(userid).and("userId").is(userId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser==null){
            recommendUser = new RecommendUser();
            recommendUser.setToUserId(userid);
            recommendUser.setUserId(userId);
            recommendUser.setScore(99d);
        }
        return recommendUser;
    }

    @Override
    public List<RecommendUser> cardsList(Long userId, int conts) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserId = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserId);
        TypedAggregation aggregation = Aggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),
                Aggregation.sample(conts));
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(aggregation, RecommendUser.class);
        List<RecommendUser> mappedResults = aggregate.getMappedResults();
        return mappedResults;
    }
}
